//
//  CoreDataManager.m
//  Lesson_5记事本
//
//  Created by lanou on 16/5/31.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "CoreDataManager.h"
@interface CoreDataManager ()
@property (nonatomic,strong)NSManagedObjectContext *context;

@end
static CoreDataManager *manager;
@implementation CoreDataManager
+ (instancetype)cteatCoreDataManager{
    if (!manager) {
        manager = [[CoreDataManager alloc]init];
    }
    return manager;
}

//- (instancetype)init{
//    self = [super init];
//    if (self) {
//        NSManagedObjectModel *model = [[NSManagedObjectModel alloc]initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"]];
//        NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
//        NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"read.sqlite"];
//        NSLog(@"%@",filePath);
//        [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:filePath] options:@{NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES} error:nil];
//        self.context.persistentStoreCoordinator = store;
//    }
//    return self;
//}
//- (NSManagedObjectContext *)context{
//    if (!_context) {
//        _context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
//    }
//    return _context;
//}
//
////查询整个文件件
//- (NSArray *)selectAllFile{
//    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"ReadModel"];
//    NSArray *array = [self.context executeFetchRequest:request error:nil];
//    return array;
//}
//
////查询有没有已经存在的文件夹
//- (BOOL)isContainsFileWithFileName:(NSString *)name{
//    
//    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"File"];
//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"name=%@",name];
//    request.predicate = pre;
//    NSArray *array = [self.context executeFetchRequest:request error:nil];
//    if (array.count==0) {
//        return NO;
//    }else{
//        return YES;
//    }
//    
//}

////创建一个文件夹
//- (File *)creatFileWithFileName:(NSString *)name WithData:(NSDate *)data{
//    BOOL judge = [self isContainsFileWithFileName:name];
//    if (judge) {
//        NSLog(@"重复添加");
//        return nil;
//    }
//    File *file = [NSEntityDescription insertNewObjectForEntityForName:@"File" inManagedObjectContext:self.context];
//    file.name = name;
//    file.time = data;
//    [self.context save:nil];
//    return file;
//}
////根据文件夹名字删除文件
//- (void)deleteFileByName:(NSString *)name{
//    
//    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"File"];
//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"name=%@",name];
//    request.predicate = pre;
//    File *a = [self.context executeFetchRequest:request error:nil].lastObject;
//    [self.context deleteObject:a];
//    [self.context save:nil];
//    
//}
//
////创建一个note文件
//- (void)insertNoteInFileByFileName:(NSString *)fileName noteName:(NSString *)noteName content:(NSString *)context date:(NSDate *)date{
//    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"File"];
//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"name=%@",fileName];
//    request.predicate = pre;
//    File *file = [self.context executeFetchRequest:request error:nil].lastObject;
//    
//    Note *note = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:self.context];
//    note.name = noteName;
//    note.time = date;
//    
//    note.content = context;
//    
//    [file addMyNotesObject:note];
//    
//    [self.context save:nil];
//    //return note;
//}
//
////通过文件夹名字获取到对应日记
//- (NSMutableArray *)selectAllNoteByFileName:(NSString *)filename{
//    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"File"];
//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"name=%@",filename];
//    request.predicate = pre;
//    File *fi = [self.context executeFetchRequest:request error:nil].firstObject;
//    NSMutableArray *arr = [NSMutableArray array];
//    for (Note *no in fi.myNotes) {
//        [arr addObject:no];
//    }
//    return arr;
//}
//
//
////修改
//- (void)updateNoteWithNote:(Note *)note withName:(NSString *)Newname withContent:(NSString *)Newcontent{
//    
//    note.name = Newname;
//    note.content = Newcontent;
//    
//    [self.context save:nil];
//    
//}
















@end
